package com.toptower.rfid_read_write;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewStub;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

import com.toptower.rfid_read_write.util.GlobalClient;
import com.toptower.rfid_read_write.util.ToastUtils;



public class MainActivity extends AppCompatActivity {
    EditText tcp_param;
    private boolean isClient = false;
    TextView connect_param;
    TextView connect_type;
    SharedPreferences sp;
    SharedPreferences.Editor edit;
    private int conType = 0;
    ViewStub tcp_stub;
    Button disConnect,connect,readorwrite;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        connect_param=findViewById(R.id.connect_param);
        disConnect=findViewById(R.id.disConnect);
        connect=findViewById(R.id.cusConnect);
        readorwrite=findViewById(R.id.readOrWrite);


        readorwrite.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                readOrWriteWay();
            }
        });
        connect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                cusConnect();
            }
        });

        disConnect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                disConnect();
            }
        });


        sp = getSharedPreferences("TCP_PARAM", Activity.MODE_PRIVATE);

    }

    public void readOrWriteWay() {
        if (isClient) {
            Intent intent = new Intent(this, Main2Activity.class);
            intent.putExtra("isClient", isClient);
            startActivity(intent);
        } else {
            ToastUtils.showText("未连接");
        }

    }

    //设备连接

    public void cusConnect() {
        final View writeView = getLayoutInflater().inflate(R.layout.connect, null, false);
     //   initViewStub(writeView);
        AlertDialog.Builder writeDialog = new AlertDialog.Builder(MainActivity.this);
        writeDialog.setView(writeView);
        writeDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                if (!isClient) {
                    connect();
                } else {
                    ToastUtils.showText("已连接，请勿重复连接");
                }
            }
        });

        AlertDialog dialog = writeDialog.create();
        dialog.show();
        dialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
        //监听连接类型 初始化会执行

        tcp_stub=writeView.findViewById(R.id.tcp_stub);
        tcp_param=writeView.findViewById(R.id.tcp_param);

        conType = 0;
        tcp_stub.setVisibility(View.VISIBLE);
        tcp_param = writeView.findViewById(R.id.tcp_param);
        tcp_param.setText(sp.getString("tcp", "127.0.0.1:8160"));

    }

    //断开连接

    public void disConnect() {
        if (isClient) {
            GlobalClient.getClient().close();
            isClient = false;
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    connect_param.setText("已断开连接");
                    connect_param.setTextColor(getResources().getColor(R.color.IndianRed));
                }
            });
        } else {
            ToastUtils.showText("未连接");
        }
    }


    //设置连接状态
    public void setConnect_param(final String msg) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (isClient) {
                    connect_param.setText(msg + " Success");
                    connect_param.setTextColor(getResources().getColor(R.color.DodgerBlue));
                } else {
                    connect_param.setText(msg + " Fail");
                    connect_param.setTextColor(getResources().getColor(R.color.IndianRed));
                }
            }
        });
    }

    //2种连接
    public void connect() {


                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        if (GlobalClient.getClient().openTcp(tcp_param.getText().toString().trim(), 0)) {
                            isClient = true;
                            edit = sp.edit();
                            edit.putString("tcp", tcp_param.getText().toString().trim());
                            edit.commit();
                            setConnect_param("Tcp " + tcp_param.getText().toString().trim());
//                            Looper.prepare();
                            ToastUtils.showLooperText(MainActivity.this, "连接成功");
//                            Looper.loop();
                        } else {
                            isClient = false;
                            setConnect_param("Tcp " + tcp_param.getText().toString().trim());
//                            Looper.prepare();
                            ToastUtils.showLooperText(MainActivity.this, "连接失败");
//                            Looper.loop();
                        }
                    }
                }).start();



    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (isClient) {
            Log.e("退出应用", "断开连接");
            GlobalClient.getClient().close();
        }
    }


}
